module.exports = {
	/**
	 * 查询多条记录 分页
	 * @url admin/recharge/pub/getList 前端调用的url参数地址
	 * data 请求参数 说明
	 * @params {Number}         pageIndex 当前页码
	 * @params {Number}         pageSize  每页显示数量
	 * @params {Array<Object>}  sortRule  排序规则
	 * @params {object}         formData  查询条件数据源
	 * @params {Array<Object>}  columns   查询条件规则
	 * res 返回参数说明
	 * @params {Number}         code      错误码，0表示成功
	 * @params {String}         msg       详细信息
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			type,
			uid,
			formData,
			_add_time,
			out_trade_no,
			pay_status
		} = data;
		let res = {
			code: 0,
			msg: ''
		};
		// 业务逻辑开始-----------------------------------------------------------
		let {
			todayStart,
			todayEnd
		} = vk.pubfn.getCommonTime(new Date());
		var time0 =todayStart;
		var time1 =todayEnd;
		let dbName = "a_recharge"; // 表名
		// res.tips = event.originalParam.context.requestId || 0
		// let addvktest = await db.collection('a_debug_log').add({
		// 	tips: "充值页面调试",
		// 	request_id: event.originalParam.context.requestId
		// })
		// console.log("formData", formData);
		if (userInfo.role.includes("admin")) {
			res = await vk.baseDao.getTableData({
				dbName,
				data
			});
			try {
				if (formData._add_time.length === 0 ) {
					delete formData._add_time;
				}
			}catch(err){
				delete formData._add_time;
			}
			try {
				if(formData.tv_name.length === 0){
					delete formData.tv_name;
				}
			}catch(err){
				delete formData.tv_name;
			}
			
			try {
				time0=formData._add_time[0] ;
				time1=formData._add_time[1] ;
				delete formData._add_time
			}catch(err){
				delete formData._add_time
			}
			
			console.log("formData56", formData);
			if (type) {
				let sum = await vk.baseDao.sum({
					dbName: "a_recharge", // 表名
					fieldName: "shifu_amount", // 需要求和的字段名
					whereJson: {
						...formData,
						_add_time: _.gte(time0).lte(time1)
					}
				});
				res.sum = sum;
			}
			return res;
		}

		if (userInfo.role.includes("middleman")) {
			res = await vk.baseDao.getTableData({
				dbName,
				data,
				whereJson: {
					middleman_id: uid
				},
			});
			try {
				if (formData._add_time.length === 0 ) {
					delete formData._add_time;
				}
			}catch(err){
				delete formData._add_time;
			}
			try {
				if(formData.tv_name.length === 0){
					delete formData.tv_name;
				}
			}catch(err){
				delete formData.tv_name;
			}
			
			try {
				time0=formData._add_time[0] ;
				time1=formData._add_time[1] ;
				delete formData._add_time
			}catch(err){
				delete formData._add_time
			}
			
			console.log("formData56", formData);
			if (type) {
				let sum = await vk.baseDao.sum({
					dbName: "a_recharge", // 表名
					fieldName: "shifu_amount", // 需要求和的字段名
					whereJson: {
						...formData,
						_add_time: _.gte(time0).lte(time1),
						middleman_id: uid
					}
				});
				res.sum = sum;
			}
			return res;
		}

		if (userInfo.role.includes("agent")) {
			res = await vk.baseDao.getTableData({
				dbName,
				data,
				whereJson: {
					invite_code: uid
				},
			});
			try {
				if (formData._add_time.length === 0 ) {
					delete formData._add_time;
				}
			}catch(err){
				delete formData._add_time;
			}
			try {
				if(formData.tv_name.length === 0){
					delete formData.tv_name;
				}
			}catch(err){
				delete formData.tv_name;
			}
			
			try {
				time0=formData._add_time[0] ;
				time1=formData._add_time[1] ;
				delete formData._add_time
			}catch(err){
				delete formData._add_time
			}
			
			console.log("formData56", formData);
			if (type) {
				let sum = await vk.baseDao.sum({
					dbName: "a_recharge", // 表名
					fieldName: "shifu_amount", // 需要求和的字段名
					whereJson: {
						...formData,
						_add_time: _.gte(time0).lte(time1),
						invite_code: uid
					}
				});
				res.sum = sum;
			}
			return res;
		}

		if (userInfo.role.includes("copyright")) {
			res = await vk.baseDao.getTableData({
				dbName,
				data,
				whereJson: {
					tv_name: formData.tv_name || "搜索剧名"
				},
			});
			return res;
		}

	}

}